﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace AutoCadConvert
{
    public class TagSoft:BspObject
    {
        public string Rotation;
        public Point BoxMax;
        public Point BoxMin;
        public string PrimitiveId;
        public string PrimitiveType;
        public TagSoft()
        {
            
        }
        public override IEnumerable<XElement> ToXml()
        {
            return base.ToXml().Concat(new XElement[] {
                new XElement("Rotation",Rotation),
                new XElement("BoxMax",BoxMax.ToMirror().To3DString()),
                new XElement("BoxMin",BoxMin.ToMirror().To3DString()),
                new XElement("PrimitiveId",PrimitiveId),
                new XElement("PrimitiveType",PrimitiveType),
            });
        }
    }
    class TagForSoft:Collection
    {
        private List<TagSoft> m_tagSoft = new List<TagSoft>();
        public TagForSoft(House house) : base(house) { }
        public override void AjustLineAndPoint()
        {
            //位置矫正
            Point center = m_house.Item<Points>().m_Center;
            foreach (BspObject obj in m_house.objList)
            {
                Collection collect = obj as Collection;
                if (collect!=null)
                {
                    foreach (BspObject item in collect.m_EntityList)
                    {
                        if (item is Block)
                        {
                            Block bref = item as Block;
                            if (bref.m_Block.GetValueAttributesInBlock("图例分类") == "软装")
                            {
                                Point basePt = new Point(bref.m_Block.Position);
                                List<Point> ptls = new List<Point>();
                                Block.RecurBlockReference(bref.m_Block,bref.m_Block.ScaleFactors,basePt,center,ref ptls);//解析数据
                                if (ptls.Count == 0) continue;
                                m_tagSoft.Add(new TagSoft()
                                {
                                    PrimitiveId = bref.m_Block.GetValueAttributesInBlock("图例编码"),
                                    PrimitiveType = bref.m_Block.GetValueAttributesInBlock("类型名称"),
                                    BoxMax = new Point() { X = ptls.Max(o => o.X), Y = ptls.Max(o => o.Y) },//镜像
                                    BoxMin = new Point() { X = ptls.Min(o => o.X), Y = ptls.Min(o => o.Y) },
                                    Rotation = "P=0 Y=" + (int)(bref.m_Block.Rotation * 180 / Math.PI) + " R=0",
                                });
                            }
                        }
                    }
                }
            }
        }
        public override IEnumerable<XElement> ToXml()
        {
            int idx = 0;
            foreach (TagSoft ts in m_tagSoft)
                yield return new XElement("__" + idx++, ts.ToXml());
        }
    }
}
